<?php
/**
 * @author jonathan <jo.link.noir@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 14-04-2010
 */
class U1_Galaxy
{
	const NUMBER_GALAXY = 5;
	const NUMBER_SYSTEM = 10;
	const COUNT_BORDER = 11;
	const NUMBER_POSITION = 121; //self::COUNT_BORDER²


	/**
	 * @param array $target : {galaxy, system, position}
	 * @param array $target2 : {galaxy, system, position}
	 * @return int
	 */
	static public function calculateDistance(array $target, array $target2)
	{
		self::realCoordinated($target[0], self::NUMBER_GALAXY);
		self::realCoordinated($target2[0], self::NUMBER_GALAXY);
		self::realCoordinated($target[1], self::NUMBER_SYSTEM);
		self::realCoordinated($target2[1], self::NUMBER_SYSTEM);
		self::realPosition($target[2]);
		self::realPosition($target2[2]);

		foreach ($target as $k => &$v)
		{
			$v -= $target2[$k];
			if ($v < 0)
				$v = -$v;
		}

		return (int)(10000 * $target[0] + 300 * $target[1] + 10 * $target[2]);
	}

	static private function realCoordinated(&$coordinated, $lenght)
	{
		if ($coordinated > $lenght / 2)
			$coordinated = $lenght - $coordinated;
	}

	static private function realPosition(&$position)
	{
		$axe = self::getAxe($position);
		self::realCoordinated($axe[0], self::NUMBER_POSITION);
		self::realCoordinated($axe[1], self::NUMBER_POSITION);
		$position = $axe[0] + $axe[1] * self::COUNT_BORDER;
	}

	/**
	 * @param int $position
	 * @return array : {column (x), line (y)}
	 */
	static function getAxe($position)
	{
		$y = (int)(($position - 1) / self::COUNT_BORDER);
		return array($position - self::COUNT_BORDER * $y++, $y);
	}
}